package app;

import java.awt.event.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;

import java.lang.ClassLoader;
import javax.swing.*;

import framework.DataBaseUtils;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.TitledBorder;

public class MainGuiRegistrationForm extends JFrame {
	private static ClassLoader cLoader = new ClassLoader() {
	};

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected static JFrame mainFrame;

	/**
	 * Creates new form ContactEditor
	 */
	public MainGuiRegistrationForm() {
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {
		// test

		registerResultFrame = new javax.swing.JFrame();
		registerResultPanel = new javax.swing.JPanel();
		registrationSuccessLabel = new javax.swing.JLabel();
		okSuccessButton = new javax.swing.JButton();
		infoResultLabel = new javax.swing.JLabel();
		infoResultFullNameLabel = new javax.swing.JLabel();
		infoResultLoginLabel = new javax.swing.JLabel();
		infoResultPasswordLabel = new javax.swing.JLabel();

		registerResultFrame
				.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		registerResultFrame.setCursor(new java.awt.Cursor(
				java.awt.Cursor.DEFAULT_CURSOR));
		registerResultFrame.setForeground(java.awt.Color.white);
		registerResultFrame.setMinimumSize(new java.awt.Dimension(450, 250));
		registerResultFrame.setLocationRelativeTo(getParent());
		registerResultFrame.setTitle("Результат регистрации");

		registerResultPanel.setMinimumSize(new java.awt.Dimension(200, 300));

		infoResultLabel
				.setText("Организация была зарегистрирована со следующими параметрами:");

		infoResultFullNameLabel.setText("Полное наименование:");

		infoResultLoginLabel.setText("Логин:");

		okSuccessButton.setText("ОК");
		okSuccessButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				okSuccessButtonActionPerformed(evt);
			}
		});

		infoResultPasswordLabel.setText("Пароль:");

		registrationSuccessLabel.setFont(new java.awt.Font("Tahoma", 3, 14)); // NOI18N
		registrationSuccessLabel.setText("Регистрация успешно завершена!");
		registrationSuccessLabel.setForeground(OK_LABEL_COLOR);

		org.jdesktop.layout.GroupLayout registerResultPanelLayout = new org.jdesktop.layout.GroupLayout(
				registerResultPanel);
		registerResultPanel.setLayout(registerResultPanelLayout);
		registerResultPanelLayout
				.setHorizontalGroup(registerResultPanelLayout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(registerResultPanelLayout
								.createSequentialGroup()
								.addContainerGap()
								.add(registerResultPanelLayout
										.createParallelGroup(
												org.jdesktop.layout.GroupLayout.LEADING)
										.add(org.jdesktop.layout.GroupLayout.TRAILING,
												registerResultPanelLayout
														.createSequentialGroup()
														.add(registrationSuccessLabel,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																270,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.add(52, 52, 52))
										.add(registerResultPanelLayout
												.createSequentialGroup()
												.add(registerResultPanelLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(infoResultLabel)
														.add(registerResultPanelLayout
																.createSequentialGroup()
																.add(29, 29, 29)
																.add(registerResultPanelLayout
																		.createParallelGroup(
																				org.jdesktop.layout.GroupLayout.LEADING)
																		.add(infoResultFullNameLabel)
																		.add(org.jdesktop.layout.GroupLayout.LEADING,
																				infoResultLoginLabel)
																		.add(org.jdesktop.layout.GroupLayout.LEADING,
																				infoResultPasswordLabel)))
														.add(org.jdesktop.layout.GroupLayout.TRAILING,
																registerResultPanelLayout
																		.createSequentialGroup()
																		.add(okSuccessButton,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																				83,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
																		.add(126,
																				126,
																				126)))
												.addContainerGap()))));
		registerResultPanelLayout.setVerticalGroup(registerResultPanelLayout
				.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(registerResultPanelLayout
						.createSequentialGroup()
						.addContainerGap()
						.add(registrationSuccessLabel)
						.add(11, 11, 11)
						.add(infoResultLabel)
						.add(18, 18, 18)
						.add(infoResultFullNameLabel,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)
						.addPreferredGap(
								org.jdesktop.layout.LayoutStyle.RELATED)
						.add(infoResultLoginLabel,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)
						.addPreferredGap(
								org.jdesktop.layout.LayoutStyle.RELATED)
						.add(infoResultPasswordLabel,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)
						.addPreferredGap(
								org.jdesktop.layout.LayoutStyle.RELATED,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE).add(okSuccessButton)
						.addContainerGap()));

		org.jdesktop.layout.GroupLayout registerResultFrameLayout = new org.jdesktop.layout.GroupLayout(
				registerResultFrame.getContentPane());
		registerResultFrame.getContentPane().setLayout(
				registerResultFrameLayout);
		registerResultFrameLayout.setHorizontalGroup(registerResultFrameLayout
				.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(registerResultFrameLayout
						.createSequentialGroup()
						.addContainerGap()
						.add(registerResultPanel,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE).addContainerGap()));
		registerResultFrameLayout.setVerticalGroup(registerResultFrameLayout
				.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(registerResultFrameLayout
						.createSequentialGroup()
						.addContainerGap()
						.add(registerResultPanel,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
								170, Short.MAX_VALUE)));

		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setTitle("Форма ввода данных регистрируемой организации");
		setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
		setMaximumSize(new Dimension(710, 850));
		setPreferredSize(new Dimension(750, 750));
		getContentPane().setLayout(null);

		JTabbedPane fstTabbedPane = new JTabbedPane(JTabbedPane.TOP);
		fstTabbedPane.setBorder(new TitledBorder(null, "xccxc",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		fstTabbedPane.setBounds(10, 6, 710, 750);
		getContentPane().add(fstTabbedPane);

		fstTabContainerPanel = new JPanel();
		fstTabContainerPanel.setBorder(null);
		fstTabbedPane.addTab("Основные параметры регистрации", null,
				fstTabContainerPanel, null);
		fstTabbedPane.setBorder(null);
		orgDataPanel = new javax.swing.JPanel();
		fullNameLabel = new javax.swing.JLabel();
		fullNameLabel.setBounds(20, 35, 132, 16);
		emailLabel = new javax.swing.JLabel();
		emailLabel.setBounds(426, 35, 32, 16);
		fullNameTextField = new javax.swing.JTextField();
		fullNameTextField.setBounds(170, 28, 218, 30);
		emailTextField = new javax.swing.JTextField();
		emailTextField.setBounds(464, 28, 218, 30);
		phoneLabel = new javax.swing.JLabel();
		phoneLabel.setBounds(101, 183, 51, 16);
		phoneTextField = new javax.swing.JTextField();
		phoneTextField.setBounds(170, 176, 218, 30);
		ogrnLabel = new javax.swing.JLabel();
		ogrnLabel.setBounds(119, 71, 33, 16);
		ogrnTextField = new javax.swing.JTextField();
		ogrnTextField.setBounds(170, 64, 218, 30);
		innLabel = new javax.swing.JLabel();
		innLabel.setBounds(125, 146, 27, 16);
		innTextField = new javax.swing.JTextField();
		innTextField.setBounds(170, 139, 218, 30);
		kppLabel = new javax.swing.JLabel();
		kppLabel.setBounds(127, 110, 25, 16);
		kppTextField = new javax.swing.JTextField();
		kppTextField.setBounds(170, 103, 218, 30);
		checkOrgDataButton = new javax.swing.JButton();
		checkOrgDataButton.setBounds(464, 176, 218, 30);
		isOgrnUniqueLabel = new javax.swing.JLabel();
		isOgrnUniqueLabel.setBounds(464, 73, 218, 14);
		isKppUniqueLabel = new javax.swing.JLabel();
		isKppUniqueLabel.setBounds(464, 112, 218, 14);
		isInnUniqueLabel = new javax.swing.JLabel();
		isInnUniqueLabel.setBounds(464, 148, 218, 14);

		orgDataPanel.setBorder(BorderFactory
				.createTitledBorder("Данные организации "));
		orgDataPanel.setPreferredSize(new java.awt.Dimension(700, 180));

		fullNameLabel.setText("Полное наименование");

		emailLabel.setText("Email");

		fullNameTextField.setText("My_AUTO test 01");
		fullNameTextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				fullNameTextFieldActionPerformed(evt);
			}
		});

		emailTextField.setText("testmailfz223@gmail.com");
		emailTextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				emailTextFieldActionPerformed(evt);
			}
		});

		phoneLabel.setText("Телефон");

		phoneTextField.setText("8-495-9938028");

		ogrnLabel.setText("ОГРН");

		ogrnTextField.setText("9999999999993");
		ogrnTextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				ogrnTextFieldActionPerformed(evt);
			}
		});

		innLabel.setText("ИНН");

		innTextField.setText("4444444493");
		innTextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				innTextFieldActionPerformed(evt);
			}
		});

		kppLabel.setText("КПП");

		kppTextField.setText("444444493");

		checkOrgDataButton.setText("Проверить уникальность ОГРН+ИНН+КПП");
		checkOrgDataButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				checkOrgDataButtonActionPerformed(evt);
			}
		});

		isOgrnUniqueLabel.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
		isOgrnUniqueLabel.setForeground(new java.awt.Color(255, 51, 51));
		isOgrnUniqueLabel.setText("isOrgnUniqueLabel TEXT");
		isOgrnUniqueLabel.setVisible(false);
		isOgrnUniqueLabel.setName("ОГРН");

		isKppUniqueLabel.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
		isKppUniqueLabel.setForeground(new java.awt.Color(255, 51, 51));
		isKppUniqueLabel.setText("isKppUniqueLabel TEXT");
		isKppUniqueLabel.setVisible(false);
		isKppUniqueLabel.setName("КПП");

		isInnUniqueLabel.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
		isInnUniqueLabel.setForeground(new java.awt.Color(255, 51, 51));
		isInnUniqueLabel.setText("isInnUniqueLabel TEXT");
		isInnUniqueLabel.setVisible(false);
		isInnUniqueLabel.setName("ИНН");
		orgDataPanel.setLayout(null);
		orgDataPanel.add(fullNameLabel);
		orgDataPanel.add(ogrnLabel);
		orgDataPanel.add(innLabel);
		orgDataPanel.add(kppLabel);
		orgDataPanel.add(phoneLabel);
		orgDataPanel.add(fullNameTextField);
		orgDataPanel.add(ogrnTextField);
		orgDataPanel.add(emailLabel);
		orgDataPanel.add(kppTextField);
		orgDataPanel.add(innTextField);
		orgDataPanel.add(phoneTextField);
		orgDataPanel.add(checkOrgDataButton);
		orgDataPanel.add(isKppUniqueLabel);
		orgDataPanel.add(isOgrnUniqueLabel);
		orgDataPanel.add(emailTextField);
		orgDataPanel.add(isInnUniqueLabel);

		createIntUserCheckBox = new JCheckBox("Создать пользователя интеграции");
		createIntUserCheckBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				int stateValue = e.getStateChange();
				if (stateValue == 1) {
					intSettingsPanel.setVisible(true);
					setIntSelected(true);
				} else {
					intSettingsPanel.setVisible(false);
					setIntSelected(true);
				}
			}
		});

		createIntUserCheckBox.setBounds(170, 221, 263, 18);
		orgDataPanel.add(createIntUserCheckBox);

		JPanel stendPanel = new JPanel();
		stendPanel.setBorder(BorderFactory.createTitledBorder("Выбор стенда"));

		JRadioButton stendKpakRadioButton = new JRadioButton(
				"КПАК (fz01.lanit.ru)");
		stendKpakRadioButton.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				int stateVal = e.getStateChange();
				if (stateVal == 1) {

					setSelectedStend(1);

				}

			}
		});
		stendKpakRadioButton.setBounds(21, 26, 125, 18);
		chooseStendButtonGroup.add(stendKpakRadioButton);

		JRadioButton stendIntRadioButton = new JRadioButton(
				"ИНТ (fz04.lanit.ru)");
		stendIntRadioButton.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				int stateVal = e.getStateChange();
				if (stateVal == 1) {

					setSelectedStend(4);

				}

			}
		});
		stendIntRadioButton.setBounds(388, 26, 125, 18);
		chooseStendButtonGroup.add(stendIntRadioButton);

		JRadioButton stendTpakRadioButton = new JRadioButton(
				"ТПАК (fz02.lanit.ru)");
		stendTpakRadioButton.setSelected(true);
		setSelectedStend(2);

		stendTpakRadioButton.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				int stateVal = e.getStateChange();
				if (stateVal == 1) {

					setSelectedStend(2);

				}
			}
		});
		stendTpakRadioButton.setBounds(198, 26, 125, 18);

		chooseStendButtonGroup.add(stendTpakRadioButton);

		stendPanel.setLayout(null);
		stendPanel.add(stendKpakRadioButton);

		stendPanel.add(stendIntRadioButton);
		stendPanel.add(stendTpakRadioButton);
		userDataPanel = new javax.swing.JPanel();
		loginLabel = new javax.swing.JLabel();
		loginLabel.setBounds(117, 35, 33, 16);
		firstNameLabel = new javax.swing.JLabel();
		firstNameLabel.setBounds(121, 107, 29, 16);
		loginTextField = new javax.swing.JTextField();
		loginTextField.setBounds(168, 28, 218, 30);
		firstNameTextField = new javax.swing.JTextField();
		firstNameTextField.setBounds(168, 100, 218, 30);
		lastNameLabel = new javax.swing.JLabel();
		lastNameLabel.setBounds(97, 71, 53, 16);
		lastNameTextField = new javax.swing.JTextField();
		lastNameTextField.setBounds(168, 64, 218, 30);
		middleNameLabel = new javax.swing.JLabel();
		middleNameLabel.setBounds(97, 143, 59, 16);
		middleNameTextField = new javax.swing.JTextField();
		middleNameTextField.setBounds(168, 136, 218, 30);
		checkUserDataButton = new javax.swing.JButton();
		checkUserDataButton.setBounds(463, 64, 218, 30);
		isLoginUniqueLabel = new javax.swing.JLabel();
		isLoginUniqueLabel.setBounds(463, 37, 218, 14);

		userDataPanel.setBorder(BorderFactory
				.createTitledBorder("Данные администратора организации "));

		loginLabel.setText("Логин");

		firstNameLabel.setText("Имя");
		firstNameLabel.setHorizontalTextPosition(SwingConstants.RIGHT);
		firstNameLabel.setInheritsPopupMenu(false);

		loginTextField.setText("223-223-223 55");
		loginTextField.setPreferredSize(new java.awt.Dimension(84, 20));
		loginTextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				loginTextFieldActionPerformed(evt);
			}
		});

		firstNameTextField.setText("MY_Имя-Тест");
		firstNameTextField.setPreferredSize(new java.awt.Dimension(84, 20));
		firstNameTextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				firstNameTextFieldActionPerformed(evt);
			}
		});

		lastNameLabel.setText("Фамилия");

		lastNameTextField.setText("MY_Фамилия-Тест");
		lastNameTextField.setPreferredSize(new java.awt.Dimension(84, 20));

		middleNameLabel.setLabelFor(middleNameTextField);
		middleNameLabel.setText("Отчество");
		middleNameLabel.setHorizontalTextPosition(SwingConstants.RIGHT);

		middleNameTextField.setText("MY_Отчество-Тест");
		middleNameTextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				middleNameTextFieldActionPerformed(evt);
			}
		});

		checkUserDataButton.setText("Проверить уникальность логина");
		checkUserDataButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				checkUserDataButtonActionPerformed(evt);
			}
		});

		isLoginUniqueLabel.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
		isLoginUniqueLabel.setForeground(new java.awt.Color(255, 51, 51));
		isLoginUniqueLabel.setText("isUserUniqueLabel TEXT");
		isLoginUniqueLabel.setVisible(false);
		isLoginUniqueLabel.setName("Логин");
		userDataPanel.setLayout(null);
		userDataPanel.add(lastNameLabel);
		userDataPanel.add(firstNameLabel);
		userDataPanel.add(loginLabel);
		userDataPanel.add(loginTextField);
		userDataPanel.add(lastNameTextField);
		userDataPanel.add(middleNameTextField);
		userDataPanel.add(firstNameTextField);
		userDataPanel.add(middleNameLabel);
		userDataPanel.add(checkUserDataButton);
		userDataPanel.add(isLoginUniqueLabel);

		JCheckBox setUserPermissionsCheckBox = new JCheckBox(
				"Проставить права пользователю");
		setUserPermissionsCheckBox.setSelected(true);
		setIsSetUserPermissions(1);
		setUserPermissionsCheckBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				setIsSetUserPermissions(e.getStateChange());
			}
		});
		setUserPermissionsCheckBox.setBounds(463, 106, 218, 18);
		userDataPanel.add(setUserPermissionsCheckBox);

		intSettingsPanel = new JPanel();
		intSettingsPanel.setBorder(BorderFactory
				.createTitledBorder("Настройки интеграции"));
		intSettingsPanel.setLayout(null);
		intSettingsPanel.setVisible(false);

		JLabel integrationUserLoginLabel = new JLabel();
		integrationUserLoginLabel.setText("Логин интеграции");
		integrationUserLoginLabel.setBounds(50, 32, 108, 16);
		intSettingsPanel.add(integrationUserLoginLabel);

		integrationUserLoginTextField = new JTextField();
		integrationUserLoginTextField.setText("223-223-55");
		integrationUserLoginTextField.setPreferredSize(new Dimension(84, 20));
		integrationUserLoginTextField.setBounds(170, 32, 218, 30);
		intSettingsPanel.add(integrationUserLoginTextField);

		JLabel integrationUserPasswordLabel = new JLabel();
		integrationUserPasswordLabel.setText("Пароль интеграции");
		integrationUserPasswordLabel.setBounds(37, 68, 121, 16);
		intSettingsPanel.add(integrationUserPasswordLabel);

		integrationUserPasswordTextField = new JTextField();
		integrationUserPasswordTextField.setText("123asdASD");
		integrationUserPasswordTextField
				.setPreferredSize(new Dimension(84, 20));
		integrationUserPasswordTextField.setBounds(170, 68, 218, 30);
		intSettingsPanel.add(integrationUserPasswordTextField);

		JCheckBox setIntPermissionsCheckBox = new JCheckBox(
				"Проставить права интеграции");
		setIntPermissionsCheckBox.setSelected(true);
		setIsSetIntPermissions(1);
		setIntPermissionsCheckBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				setIsSetIntPermissions(e.getStateChange());
			}
		});
		setIntPermissionsCheckBox.setBounds(462, 38, 218, 18);
		intSettingsPanel.add(setIntPermissionsCheckBox);
		registerButton = new javax.swing.JButton();

		registerButton.setText("Зарегистрировать");
		cancelButton = new javax.swing.JButton();

		cancelButton.setText("Отмена");
		clearFormButon = new javax.swing.JButton();

		clearFormButon.setText("Очистить форму");
		GroupLayout gl_fstTabContainerPanel = new GroupLayout(
				fstTabContainerPanel);
		gl_fstTabContainerPanel
				.setHorizontalGroup(gl_fstTabContainerPanel
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_fstTabContainerPanel
										.createSequentialGroup()
										.addGap(6)
										.addGroup(
												gl_fstTabContainerPanel
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(
																stendPanel,
																GroupLayout.PREFERRED_SIZE,
																700,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																orgDataPanel,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)))
						.addGroup(
								gl_fstTabContainerPanel
										.createSequentialGroup()
										.addGap(6)
										.addComponent(userDataPanel,
												GroupLayout.PREFERRED_SIZE,
												700, GroupLayout.PREFERRED_SIZE))
						.addGroup(
								gl_fstTabContainerPanel
										.createSequentialGroup()
										.addGap(6)
										.addComponent(intSettingsPanel,
												GroupLayout.PREFERRED_SIZE,
												700, GroupLayout.PREFERRED_SIZE))
						.addGroup(
								gl_fstTabContainerPanel
										.createSequentialGroup()
										.addGap(278)
										.addComponent(registerButton)
										.addGap(41)
										.addComponent(cancelButton)
										.addGap(33)
										.addComponent(clearFormButon,
												GroupLayout.PREFERRED_SIZE,
												137, GroupLayout.PREFERRED_SIZE)));
		gl_fstTabContainerPanel
				.setVerticalGroup(gl_fstTabContainerPanel
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_fstTabContainerPanel
										.createSequentialGroup()
										.addGap(6)
										.addGroup(
												gl_fstTabContainerPanel
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(
																stendPanel,
																GroupLayout.PREFERRED_SIZE,
																58,
																GroupLayout.PREFERRED_SIZE)
														.addGroup(
																gl_fstTabContainerPanel
																		.createSequentialGroup()
																		.addGap(56)
																		.addComponent(
																				orgDataPanel,
																				GroupLayout.PREFERRED_SIZE,
																				261,
																				GroupLayout.PREFERRED_SIZE)))
										.addComponent(userDataPanel,
												GroupLayout.PREFERRED_SIZE,
												181, GroupLayout.PREFERRED_SIZE)
										.addComponent(intSettingsPanel,
												GroupLayout.PREFERRED_SIZE,
												115, GroupLayout.PREFERRED_SIZE)
										.addGap(14)
										.addGroup(
												gl_fstTabContainerPanel
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(
																registerButton,
																GroupLayout.PREFERRED_SIZE,
																30,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																cancelButton,
																GroupLayout.PREFERRED_SIZE,
																30,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																clearFormButon,
																GroupLayout.PREFERRED_SIZE,
																30,
																GroupLayout.PREFERRED_SIZE))));
		fstTabContainerPanel.setLayout(gl_fstTabContainerPanel);
		clearFormButon.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				clearFormButonActionPerformed(evt);
			}
		});
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				cancelButtonActionPerformed(evt);
			}
		});
		registerButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				registerButtonActionPerformed(evt);
			}
		});

		getAccessibleContext().setAccessibleName("RegisterOrganizationForm");

		// Рисуем всплывающее окно об успешной регистрации

		okSuccessButton.setText("Ок");
		okSuccessButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				okSuccessButtonActionPerformed(evt);
			}
		});

		infoResultLabel
				.setText("Организация была зарегистрирована со следующими параметрами:");

		infoResultFullNameLabel.setText("Полное наименование:");

		infoResultLoginLabel.setText("Логин:");

		infoResultPasswordLabel.setText("Пароль:");

		pack();

	}// </editor-fold>

	protected void okSuccessButtonActionPerformed(ActionEvent evt) {
		registerResultFrame.dispose();

	}

	private void checkOrgDataButtonActionPerformed(ActionEvent evt) {
		initializeInputData();

		DataBaseUtils.checkOrganizationIsUnique(orgFullName, orgOgrn, orgKpp,
				orgInn);
		displayCheckResults(DataBaseUtils.OGRN_CHECK_RESULT, isOgrnUniqueLabel);
		displayCheckResults(DataBaseUtils.INN_CHECK_RESULT, isInnUniqueLabel);
		displayCheckResults(DataBaseUtils.KPP_CHECK_RESULT, isKppUniqueLabel);

	}

	private void checkUserDataButtonActionPerformed(ActionEvent evt) {
		initializeInputData();
		DataBaseUtils.checkUserIsUnique(userLogin);
		displayCheckResults(DataBaseUtils.LOGIN_CHECK_RESULT,
				isLoginUniqueLabel);
	}

	private void registerButtonActionPerformed(ActionEvent evt) {

		// инициализирует данные из формы
		initializeInputData();

		// System.out.println("cLoader.getResource(\"selenium.properties\") = "
		// + cLoader.getClass().getResource("selenium.properties"));
		// вставляет адрес организации
		db.insertOrgAddress();
		// получаем ИД вставленного адреса по зип_коду
		orgAddressId = DataBaseUtils
				.getAddressId(DataBaseUtils.ZIP_CODE_REPLACEMENT);
		// вставляет организацию
		db.insertOrganization(orgFullName, orgOgrn, orgInn, orgKpp,
				orgAddressId, orgPhone, orgWebsite, orgEmail, isIntSelected);
		// вставляет пользователя
		db.insertUser(userLogin, userFirstName, userMiddleName, userLastName,
				orgFullName, orgOgrn);

		if (getIsSetUserPermissions() == 1) {
			orgId = db.getCustomerId(orgFullName, orgOgrn);
			userId = db.getUserId(userLogin, orgOgrn, 0);
			db.insertUserPermissions(userId, orgId);
		}

		if (isIntSelected()) {
			userIntLogin = integrationUserLoginTextField.getText();
			userIntPassword = integrationUserPasswordTextField.getText();
			db.insertFakeUser(userIntLogin, userIntPassword, orgFullName,
					orgOgrn);
			boolean b;
			if (getIsSetIntPermissions() == 1)
				b = true;
			else
				b = false;

			db.insertIntSettings(orgFullName, orgOgrn, b);
			db.insertIntUser(userIntLogin, userIntPassword, orgFullName,
					orgOgrn);
		}
		displayResultWindow();

	}

	private void initializeInputData() {
		orgFullName = fullNameTextField.getText();
		orgEmail = emailTextField.getText();
		orgKpp = kppTextField.getText();
		orgOgrn = ogrnTextField.getText();
		orgInn = innTextField.getText();
		orgPhone = phoneTextField.getText();
		orgWebsite = "www.test.com";
		userLogin = loginTextField.getText();
		userFirstName = firstNameTextField.getText();
		userLastName = lastNameTextField.getText();
		userMiddleName = middleNameTextField.getText();
	}

	private void displayCheckResults(String resultKey, JLabel label) {
		String checkResult = DataBaseUtils.getCheckResults(resultKey);
		if (checkResult.equals(DataBaseUtils.CHECK_RESULT_OK)) {
			label.setVisible(true);
			label.setForeground(OK_LABEL_COLOR);
			label.setText(checkResult);
		} else {
			label.setVisible(true);
			label.setForeground(ERR_LABEL_COLOR);
			label.setText("Реквизит" + label.getName() + " занят :(");
		}

	}

	/*
	 * private String getLabelResultName(JLabel label) { String str =
	 * label.getName(); String result = ""; switch (str) { case
	 * "isOgrnUniqueLabel": result = "ОГРН"; break; case "isKppUniqueLabel":
	 * result = "КПП"; break; case "isInnUniqueLabel": result = "ИНН"; break;
	 * case "isUserUniqueLabel": result = "ИНН"; break;
	 * 
	 * default: break; } return result;
	 * 
	 * }
	 */

	private void displayResultWindow() {
		registerResultFrame.setVisible(true);
		infoResultFullNameLabel.setText("Полное наименование: " + orgFullName);
		infoResultLoginLabel.setText("Логин: " + userLogin);
		infoResultPasswordLabel.setText("Пароль: " + userPassword);
	}

	private void cancelButtonActionPerformed(ActionEvent evt) {
		dispose();
	}

	private void clearFormButonActionPerformed(ActionEvent evt) {
		// clearTextFields(this);
		// String s = db.getUserId("223-223 04", "9999999999925", 1) ;
		// String o = db.getCustomerId("Тестовая организация 25",
		// "9999999999925");
		initializeInputData();
		// db.insertFakeUser("222-333", "222-333", orgFullName, orgOgrn);
		// db.insertIntSettings(orgFullName, orgOgrn, true);
		// db.insertIntUser("222-333", "222-333", orgFullName, orgOgrn);

	}

	void clearTextFields(Container container) {
		for (Component c : container.getComponents()) {
			if (c instanceof JTextField) {
				((JTextField) c).setText("");
			} else if (c instanceof Container) {
				clearTextFields((Container) c);
			}
		}
	}

	// Finds the size of the screen
	private static void findScreenSize() {
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension dim = toolkit.getScreenSize();
		ScreenHeight = dim.height;
		ScreenWidth = dim.width;
	}

	// Centres the dialogue box within the screen
	private static void centerFrame(JFrame Frame, boolean Width) {
		findScreenSize();

		if (!Width) { // if you are not justifying on the X axis
			Frame.setLocation(Frame.getLocationOnScreen().x,
					((ScreenWidth / 2) - (Frame.getWidth() / 2)));
		} else {
			Frame.setLocation(((ScreenWidth / 2) - (Frame.getWidth() / 2)),
					((ScreenHeight / 2) - (Frame.getHeight() / 2)));
		}
	}

	private void fullNameTextFieldActionPerformed(ActionEvent evt) {
	}

	private void emailTextFieldActionPerformed(ActionEvent evt) {
	}

	private void ogrnTextFieldActionPerformed(ActionEvent evt) {
	}

	private void loginTextFieldActionPerformed(ActionEvent evt) {
	}

	private void firstNameTextFieldActionPerformed(ActionEvent evt) {
	}

	private void middleNameTextFieldActionPerformed(ActionEvent evt) {
	}

	private void innTextFieldActionPerformed(ActionEvent evt) {
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		// <editor-fold defaultstate="collapsed"
		// desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase
		 * /tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			UIManager.LookAndFeelInfo[] installedLookAndFeels = UIManager
					.getInstalledLookAndFeels();
			for (int idx = 0; idx < installedLookAndFeels.length; idx++)
				if ("Nimbus".equals(installedLookAndFeels[idx].getName())) {
					UIManager.setLookAndFeel(installedLookAndFeels[idx]
							.getClassName());
					break;
				}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(
					MainGuiRegistrationForm.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(
					MainGuiRegistrationForm.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(
					MainGuiRegistrationForm.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(
					MainGuiRegistrationForm.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				mainFrame = new MainGuiRegistrationForm();
				mainFrame.setVisible(true);
				centerFrame(mainFrame, true);
			}
		});
	}

	public static int getSelectedStend() {
		return selectedStend;
	}

	public static void setSelectedStend(int selectedStend) {
		MainGuiRegistrationForm.selectedStend = selectedStend;
	}

	public boolean isIntSelected() {
		return isIntSelected;
	}

	public void setIntSelected(boolean isIntSelected) {
		this.isIntSelected = isIntSelected;
	}

	public static int getIsSetUserPermissions() {
		return isSetUserPermissions;
	}

	public static void setIsSetUserPermissions(int isSetUserPermissions) {
		MainGuiRegistrationForm.isSetUserPermissions = isSetUserPermissions;
	}

	private static int getIsSetIntPermissions() {
		return isSetIntPermissions;
	}

	private static void setIsSetIntPermissions(int isSetIntPermissions) {
		MainGuiRegistrationForm.isSetIntPermissions = isSetIntPermissions;
	}

	// Variables declaration - do not modify
	private JButton cancelButton, checkOrgDataButton, checkUserDataButton,
			clearFormButon, registerButton, okSuccessButton;
	private JLabel emailLabel, firstNameLabel, fullNameLabel, innLabel,
			kppLabel, lastNameLabel, loginLabel, middleNameLabel, ogrnLabel,
			phoneLabel, isOgrnUniqueLabel, isKppUniqueLabel, isInnUniqueLabel,
			isLoginUniqueLabel, registrationSuccessLabel, infoResultLabel,
			infoResultFullNameLabel, infoResultLoginLabel,
			infoResultPasswordLabel;

	private JTextField emailTextField, firstNameTextField, fullNameTextField,
			innTextField, kppTextField, lastNameTextField, loginTextField,
			middleNameTextField, ogrnTextField, phoneTextField;

	private JPanel userDataPanel, orgDataPanel, registerResultPanel;

	private static String orgId, orgFullName, orgOgrn, orgInn, orgKpp,
			orgEmail, orgAddressId, orgPhone, orgWebsite, userLogin, userId,
			userFirstName, userLastName, userMiddleName, userIntLogin,
			userIntPassword;

	// Дефолтные значения
	private static String userPassword = "11111";

	private static JFrame registerResultFrame;

	private static int ScreenWidth, ScreenHeight, selectedStend,
			isSetUserPermissions, isSetIntPermissions;

	private DataBaseUtils db = new DataBaseUtils();

	private static final Color OK_LABEL_COLOR = new Color(0, 0, 255);
	private static final Color ERR_LABEL_COLOR = new Color(255, 51, 51);
	private final ButtonGroup chooseStendButtonGroup = new ButtonGroup();
	private JCheckBox createIntUserCheckBox;
	private JPanel intSettingsPanel;
	private JTextField integrationUserLoginTextField;
	private JTextField integrationUserPasswordTextField;
	private JPanel fstTabContainerPanel;
	private boolean isIntSelected = false;

}
